<?php

namespace App\Http\Controllers;
use App\Match;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\storage;

class MatchController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $matches = Match::latest('created_at')->paginate(8);
         return view('Match.match', compact('matches'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('Match.createMatch');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $datas=[
        'match_name'=>'required',
        'match_intro'=>'required',
        'match_picture'=>'required',
        'match_ip'=>'required',
        'match_date'=>'required',
        ];
        $this->validate($request,$datas);
        $this->uploadPicture($request);
        $matchPic=$request->get('match_name').'.'.$request->file('match_picture')->getClientOriginalExtension();
        $matches=new Match;
        $matches->match_name=$request->get('match_name');
        $matches->match_intro=$request->get('match_intro');
        $matches->match_picture=$matchPic;
        $matches->match_ip=$request->get('match_ip');
        $matches->match_date=$request->get('match_date');
        $matches->save();

        session()->flash('success','比赛创建成功！');
        return redirect()->route('match.index');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $matches = Match::findOrFail($id);
        return view('Match.editMatch',compact('matches'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $matches=Match::findOrFail($id);
        if($request->file('match_picture')){
            $Picture=$matches->match_picture;
            if($Picture!='default.jpg'){
                Storege::disk('uploads')->exists($Picture);
                Storage::disk('uploads')->delete($Picture);
            }
            $this->uploadPicture($request);
            $matches->match_picture=$request->get('match_name'). '.' . $request->file('match_picture')->getClientOriginalExtension();
            $matches->save();
        }
        $matches->update([
            $matches->match_name=$request->get('match_name'),
            $matches->match_intro=$request->get('match_intro'),
            $matches->match_ip=$request->get('match_ip'),
            $matches->match_date=$request->get('match_date')
        ]);
        $matches->save();
        session()->flash('success','更新活动成功');
        return redirect()->route('match.index');
    }
    

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $match = Match::findOrFail($id);
        $Picture=$match->match_picture;
        if($Picture!='default.jpg'){
            Storage::disk('uploads')->exists($Picture);
            Storage::disk('uploads')->exists($Picture);
        }
        $match->delete();
        session()->flash('success', '删除培训成功');
        return redirect()->route('match.index');
    }
    public function uploadPicture(Request $request){
        if($request->isMethod('post') OR $request->isMethod('patch')){
            $Picture=$request->file('match_picture');
            if($Picture->isValid()){
                $realPath=$Picture->getRealPath();
                $ext=$Picture->getClientOriginalExtension();
                $matchName=$request->get('match_name');
                $fileName=$request->get('match_name').'.'.$ext;
                Image::make($Picture)->fit(300,300)->save(public_path('/uploads/avatars/').$fileName);
            }
        }
        //
    }
}
